package spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * @Author Jeremy Zheng
 * @Date 2021/3/18 16:27
 * @Version 1.0
 */
object SparkSQL02_UDF_Demo1 {
  def main(args: Array[String]): Unit = {
    //配置spark运行环境
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("UDF")
    val spark: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
    import spark.implicits._

    val df: DataFrame = spark.read.json("dataSet/test.json")
    df.createOrReplaceTempView("user")

    spark.udf.register("prefixName",(name:String)=>{
      "Name:"+name
    })

    spark.sql("select prefixName(username),age from user").show()


    //关闭资源
    spark.close()
  }
}
